package com.a214.yxz.yuejian013.Fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;

import com.a214.yxz.yuejian013.Activity.ShowDetailActivity;
import com.a214.yxz.yuejian013.Activity.ShowSentActivity;
import com.a214.yxz.yuejian013.Base.BaseSFragment;
import com.a214.yxz.yuejian013.JavaBean.ColBean;
import com.a214.yxz.yuejian013.JavaBean.ShaituBean;
import com.a214.yxz.yuejian013.Listener.ServiceListener;
import com.a214.yxz.yuejian013.Model.ResModel;
import com.a214.yxz.yuejian013.R;
import com.a214.yxz.yuejian013.Adapter.ShowListAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link BaseSFragment} subclass.
 */
public class ShowFragment extends BaseSFragment {

    private ImageButton addShow_btn;
    private Spinner spinner;
    private ArrayAdapter<String> spinnerAdapter;
    private ArrayList<String> type = new ArrayList<String>();
    private String[] typeArray = new String[100];
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private ShowListAdapter adapter;
    private ResModel model = new ResModel("gson", getSessionID());
    private ServiceListener<List<ColBean>> col_listener;
    private ServiceListener<List<ShaituBean>> show_listener;
    private List<ColBean> colList;
    private String fan_id;


    public ShowFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_show, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        addShow_btn = (ImageButton) view.findViewById(R.id.show_addShow_btn);
        spinner = (Spinner) view.findViewById(R.id.show_spinner);
        recyclerView = (RecyclerView) view.findViewById(R.id.show_list);
        adapter = new ShowListAdapter(getContext());
        layoutManager = new GridLayoutManager(getContext(), 3);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);

        typeArray[0] = "全部晒图";
        type.add(typeArray[0]);


        col_listener = new ServiceListener<List<ColBean>>() {
            @Override
            public void onResponse(List<ColBean> l) {
                for (int i = 0; i < l.size(); i++) {
                    typeArray[i + 1] = l.get(i).getName();
                    type.add(typeArray[i + 1]);
                }
                colList = l;
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
            }
        };


        show_listener = new ServiceListener<List<ShaituBean>>() {
            @Override
            public void onResponse(final List<ShaituBean> l) {
                adapter.setList(l);
                adapter.setOnItemClickListener(new ShowListAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        Intent intent = new Intent(getContext(), ShowDetailActivity.class);
                        intent.putExtra("show_id", l.get(position).getId());
                        getContext().startActivity(intent);
                    }

                    @Override
                    public void onItemLongClick(View view, int position) {

                    }
                });
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
            }
        };


        model.getColList(getUserId(), "1", col_listener);

        //获取关注俱乐部名称添加到type后面

        spinnerAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_dropdown_item_1line, type);
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        spinner.setAdapter(spinnerAdapter);
        spinner.setSelection(0);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if (i != 0) {
                    for (int j = 0; j < colList.size(); j++) {
                        if (type.get(i).equals(colList.get(j).getName()))
                            fan_id = colList.get(j).getFan_id();
                    }
                    model.getShaituList(fan_id,1,show_listener);
                }else{
                    model.getShaituList("",1,show_listener);
                }
                Toast.makeText(getContext(), type.get(i), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });


        addShow_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), ShowSentActivity.class);
                getContext().startActivity(intent);
            }
        });
    }
}
